/*
 * Gepard 
 *
 * js/gepard.js
 *
 * Javascript Gepard tools
 *
 * Copyright (C) 2010, Taras Kudla
 *
 */

var Gepard = new Object();

/*
 * Params Setting
 */
Gepard.Caller_ID	= null;
Gepard.Caller_Pamas	= null;
Gepard.apiurl			= ___+____;
Gepard.FlagToBuffer = false;
Gepard.QueryBuffer	= "empty";

/*
 * Gepard Methods
 */
Gepard.Redirect= function (link) {
	window.location=link;
}

Gepard.debug = function(message){
    alert("Gepard js debug: "+message);
}

/*
 * Check, if Gepard js environment works correctly
 */
Gepard.selfcheck = function() {
    if( this.apiurl !=undefined){
        return true;
    } else {
        return false;
    }
}


// Loading content by defaut action
Gepard.processaction = function (Action,Params){

    this.Caller_ID=Action;
    this.Caller_Params=Params;
    if(Params=="&")Params="";
    var link=this.apiurl+"?response=json&action="+Action+Params;
    parent.location.hash=Action.replace("&","#a-");

    var callerror=false;
    var query = $.ajax({
	datatype: "json",
	url: link,
	error: function(request,text){callerror=true;alert("error in ajax query: '"+link+"',\n\ndescription: "+text);},
	async: false
    });
    if( callerror){
	return false;
    }
    var obj = $.parseJSON(query.responseText);
    if( obj.error != undefined){
	alert("Error in ajax call to api: "+obj.error);
	return false;
    }
    if( obj.redirect !=undefined){
	Gepard.redirect(obj.redirect);
	return true;
    }
    // parsing response
    $.each(obj, function( i,area){
	$.each(area,function(j,item){
	    alert(item.action);
	});
    });
}

/* ----------------------------------------------------------------------------
 * Gets Content by action and returns it
 */
Gepard.GetContent = function (Action,Params){

    var link=this.apiurl+"?response=json&call="+Action+"&"+Params;
    var callerror=false;
    var data = $.ajax({
	url: link,
	datatype: "json",
	error: function(request,text){callerror=true;alert("error in ajax query: '"+link+"',\n\ndescription: "+text);},
	async: false
    }).responseText;
    if( callerror ){
	return false;
    }
    var obj = $.parseJSON(data);
    if( obj.error != undefined){
	alert("Error in ajax call to api: "+obj.error);
	return false;
    }
    if( obj.redirect !=undefined){
	Gepard.redirect(obj.redirect);
	return true;
    }
    return obj.response;
}
/* ----------------------------------------------------------------------------
 * Alias to Gepard.GetContent
 */
Gepard.call = function ( action,params){
    return Gepard.GetContent(action, params);
}

Gepard.renderPrepare = function(data){
    
}




/*
 *  Handling functions
 */
Gepard.handleSubData=function(data,area){
	$(data).find("item").each(function () {
		var id=$(this).attr("id");
		var text=$(this).text();
		
		var object=document.getElementById("function_"+id);
		
		if(object==null){
			var a=$("#output_"+area);
			$(a).hide();
			a.empty();
			a.append(text);
			$(a).fadeIn();
		}
		
		else {
			$("#function_"+id).empty();
			$("#function_"+id).append(text);
		}
		
	});
	
}
Gepard.HandleContent=function(data)
	{
	
	/*
	 * Checking for redirections
	 */
		 
	$(data).find("redirect").each( function() {var link=$(this).text();Gepard.Redirect(link);} );

	
	/*
	 * Normal Handling
	 */
	
	$(data).find("items").each( function() {
		var text=$(this).find("data").get();
		var area=$(this).find("area").text();
		
		Gepard.handleSubData(text,area);
					
	});
	
	$(data).find("ext").each(function() {
		loadextjs($(this).text());
		});
	$(data).find("extcss").each(function() {
		loadjscssfile($(this).text(),"css");
		});
		
	}

/*
 * Other functions
 */
Gepard.redirect=function(link){
	document.body.innerHTML="<div id='jloading' style='position:fixed;top:0px;right:0px;z-index:2000;'><span style='color:white;background-color:#990000;padding:2px;'>Redirection...</span></div>";
	window.location=link;
}

//var data = Gepard.processaction("users-enter",null);
//alert(data);